package cn.edu.zucc.service.impl;

import cn.edu.zucc.entity.TComment;
import cn.edu.zucc.mapper.TCommentMapper;
import cn.edu.zucc.service.TCommentService;
import org.springframework.stereotype.Service;
import top.starp.util.ReturnT;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @description t_comment
 * @author zhengkai.blog.csdn.net
 * @date 2022-04-17
 */
@Service
public class TCommentServiceImpl implements TCommentService {

	@Resource
	private TCommentMapper tCommentMapper;


	@Override
	public Object insert(TComment tComment) {

		// valid
		if (tComment == null) {
			return ReturnT.error("必要参数缺失");
        }

		tCommentMapper.insert(tComment);
        return ReturnT.success();
	}

    @Override
    public Object delete(int id) {
        int ret = tCommentMapper.delete(id);
        return ret>0?ReturnT.success():ReturnT.error();
    }


//    @Override
//	public Object delete(Integer id) {
//		int ret = tCommentMapper.delete(id);
//		return ret>0?ReturnT.success():ReturnT.error();
//	}


	@Override
	public Object update(TComment tComment) {
		int ret = tCommentMapper.update(tComment);
		return ret>0?ReturnT.success():ReturnT.error();
	}

//    @Override
//    public TComment getById(int id) {
//        return null;
//    }


    @Override
	public TComment getById(Integer id) {
		return tCommentMapper.getById(id);
	}

@Override
public Object save(TComment tComment) {
Integer id = tComment.getId();
if (id==null) {
int insert = tCommentMapper.insert(tComment);
return ReturnT.success();
}
TComment load = tCommentMapper.getById(tComment.getId());
if (load==null) {
int insert = tCommentMapper.insert(tComment);

return ReturnT.success();
}
tCommentMapper.update(tComment);
return  ReturnT.success();
}


	@Override
	public Map<String,Object> pageList(int offset, int pageSize) {

		List<TComment> pageList = tCommentMapper.pageList(offset, pageSize);
Integer totalCount = tCommentMapper.pageListCount(offset, pageSize);

		// result
		Map<String, Object> result = new HashMap<String, Object>();
		//result.put("pageList", pageList);
		result.put("content", pageList);
		result.put("totalCount", totalCount);

		return result;
	}

}
